package com.hejing.bpeldesign.bpel;

import com.hejing.bpeldesign.model.BpelEdge;
import com.hejing.bpeldesign.model.BpelNode;
import com.hejing.bpeldesign.model.BpelProcessModel;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorLocation;
import com.intellij.openapi.fileEditor.FileEditorState;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeListener;
import java.io.InputStream;

public class BpelDiagramEditor implements FileEditor {

    private final JPanel mainPanel;
    private final VirtualFile file;

    public BpelDiagramEditor(VirtualFile file) {
        this.mainPanel = new JPanel(new BorderLayout());
        this.file = file;

        try (InputStream is = file.getInputStream()) {
            BpelProcessModel model = BpelParser.parse(is);
            BpelDiagramPanel diagramPanel = new BpelDiagramPanel(model.getNodes(), model.getEdges());
            mainPanel.add(new JScrollPane(diagramPanel), BorderLayout.CENTER);
        } catch (Exception e) {
            JLabel error = new JLabel("Failed to parse BPEL file: " + e.getMessage());
            mainPanel.add(error, BorderLayout.CENTER);
        }
    }

    @Override
    public @NotNull VirtualFile getFile() {
        return file;
    }

    @Override
    public JComponent getComponent() {
        return mainPanel;
    }

    @Override
    public JComponent getPreferredFocusedComponent() {
        return mainPanel;
    }

    @Override
    public String getName() {
        return "BPEL Diagram";
    }

    @Override
    public void setState(@NotNull FileEditorState fileEditorState) {
        // no-op
    }

    @Override
    public boolean isModified() {
        return false;
    }

    @Override
    public boolean isValid() {
        return true;
    }

    @Override
    public void addPropertyChangeListener(@NotNull PropertyChangeListener propertyChangeListener) {
        // no-op
    }

    @Override
    public void removePropertyChangeListener(@NotNull PropertyChangeListener propertyChangeListener) {
        // no-op
    }

    @Override
    public void dispose() {
        // no-op
    }

    @Override
    public <T> @Nullable T getUserData(@NotNull Key<T> key) {
        return null;
    }

    @Override
    public <T> void putUserData(@NotNull Key<T> key, @Nullable T value) {
        // no-op
    }
}
